home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / gui / gui4cli.lha / Gui4Cli / Ext / Exmpl_src / 2_GCHost / GCHost.c < prev    next >
C/C++ Source or Header  |  1998-10-22  |  5KB  |  179 lines

  1.  
  2. /***********************************************************************
  3. *
  4. *    GCHost 1.0 - D. Keletsekis (27/8/98)
  5. *
  6. *    This type of program can act as a host for Gui4Cli commands.
  7. *
  8. *    What it does, is create a public message port called "MyPort"
  9. *    (port names are case sensitive!) and wait for commands from 
  10. *    Gui4Cli, sent via the CALL command, to this port. 
  11. *
  12. *    When a command is received, it is executed, and may also return 
  13. *    some result back to Gui4Cli.
  14. *
  15. *    This example has 4 possible commands it can handle :
  16. *    QUIT    - will quit this program
  17. *    GUIS    - will print out a list of all loaded guis
  18. *    ARGS    - will print out a list of all arguments we received
  19. *    HELLO    - will send a return buffer to Gui4Cli which can
  20. *          be accessed from within Gui4Cli via $$CALL.RET
  21. *
  22. ************************************************************************/
  23.  
  24. #include <exec/exec.h>
  25. #include <exec/execbase.h>
  26. #include <exec/memory.h>
  27. #include <dos/dosextens.h>
  28. #include <dos/rdargs.h>
  29. #include <dos/dostags.h>
  30. #include <string.h>
  31. #include <stdio.h>
  32. #include <ctype.h>
  33. #include <dos.h>
  34. #include <proto/dos.h>
  35. #include <proto/exec.h>
  36. #include <graphics/text.h>
  37.  
  38. #include <Gui4Cli.h>
  39.  
  40. // prototypes
  41. struct MsgPort *openport (char *);
  42. void closeport (struct MsgPort *);
  43.  
  44. // ===============================================================
  45. //     MAIN()
  46. // ===============================================================
  47.  
  48. main()
  49. {
  50. struct MsgPort *myport=NULL;        // our port
  51. struct g4cmsg  *msg;    // Gui4Cli message pointer
  52. struct guifile *gf;     // Gui4Cli Gui file pointer
  53. int    rc = 10;         // return code
  54. BOOL   endflag = 0;    // control flag
  55. LONG   c;
  56.  
  57. // --------------------- Open message port or die..
  58.  
  59. if (!(myport = openport ("MyPort")))
  60. {   PutStr ("Couldn't open CedBar port!\n");
  61.     goto endprog;
  62. }
  63.  
  64. // ---------------------- Main wait() & process loop
  65.  
  66. while (!endflag)
  67. {
  68.    WaitPort (myport);
  69.    msg = (struct g4cmsg *)GetMsg(myport);
  70.  
  71.    // check that the message is like we expect it..
  72.    if ((msg->magic != 392001) || (msg->type != GM_COMMAND) || (!msg->gcmain) || (!msg->com))
  73.    {   msg->res = 20;  // indicate error
  74.        goto endloop;   // skip message
  75.    }
  76.  
  77.    // --------------- Parse & execute the commands..
  78.    // The command name is in msg->com and it's already converted
  79.    // into upper case by Gui4Cli.
  80.  
  81.    if (!strcmp(msg->com, "QUIT"))        // quit
  82.    {
  83.     ++endflag;
  84.    }
  85.  
  86.    else if (!strcmp(msg->com, "GUIS"))    // print the names of all the guis
  87.    {
  88.     for (gf = msg->gcmain->topguifile; gf; gf = gf->next)
  89.         Printf ("FILE: %s\n", gf->name);
  90.    }
  91.  
  92.    else if (!strcmp(msg->com, "ARGS"))    // print any arguments we received
  93.    {
  94.     for (c = 0; msg->args[c] && (c < 6); ++c)
  95.     {
  96.         Printf ("Argument %ld : %s\n", c, msg->args[c]);
  97.     }
  98.    }
  99.  
  100.    else if (!strcmp(msg->com, "HELLO"))    // answer back..
  101.    {
  102.     // here we allocate a buffer which we attach to the message 
  103.     // we received which will be sent back to Gui4Cli. The
  104.     // contents of the buffer will be accessible from within
  105.     // Gui4Cli via the $$CALL.RET internal variable.
  106.     // NOTE : We MUST use AllocVec() to get the buffer.
  107.     // NOTE2: Gui4Cli is responsible for freeing our buffer
  108.  
  109.     if (msg->msgret = (UBYTE *)AllocVec (50, MEMF_CLEAR))
  110.     {
  111.        strcpy (msg->msgret, "Hello there Gui4Cli!");
  112.     }
  113.    }
  114.  
  115.    // ... you can add more commands here ...
  116.  
  117.    endloop:
  118.  
  119.    // reply the message to Gui4Cli
  120.    ReplyMsg ((struct Message *)msg);
  121.  
  122. }  // end of main while(!endflag) loop
  123.  
  124. rc = 0; // everything ok..
  125.  
  126. // ---------------------- END PROG - CLEAN UP
  127.  
  128. endprog :
  129. if (myport) closeport (myport);
  130. return (rc);
  131. }
  132.  
  133. // ================================================================
  134. //     create a new public message port
  135. // ================================================================
  136.  
  137. struct MsgPort *openport (char *portname)
  138. {
  139.    struct MsgPort *port=NULL;
  140.  
  141.    Forbid ();
  142.    if ((port = FindPort(portname)) != NULL)  
  143.    {    // if port already exists - return NULL
  144.         port = NULL;
  145.    }
  146.    else
  147.         port = CreatePort (portname, 0);
  148.    Permit();
  149.  
  150.    return (port);
  151. }
  152.  
  153. // ================================================================
  154. //     free a public message port
  155. // ================================================================
  156.  
  157. void closeport (struct MsgPort *port)
  158. {
  159.    struct Message *msg;
  160.  
  161.    Forbid ();
  162.    // empty port
  163.    while (msg = GetMsg (port))
  164.           ReplyMsg (msg);
  165.    // remove port name since it's a public msg port
  166.    if (port->mp_Node.ln_Name)
  167.           RemPort (port);
  168.    // delete port
  169.    DeleteMsgPort (port);
  170.    Permit ();
  171. }
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.